#include <clutter/clutter.h>
#include <stdlib.h>

static gboolean
on_stage_button_press (ClutterStage *stage, ClutterEvent *event, gpointer data)
{
	gfloat x = 0;
	gfloat y = 0;
	clutter_event_get_coords (event, &x, &y);

	g_print ("Clicked stage at (%f, %f)\n", x, y);

	ClutterActor *rect = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, x, y);
	if (!rect)
		return FALSE;

	if (CLUTTER_IS_RECTANGLE (rect))
		g_print ("A rectangle is at that position.\n");

	return TRUE;
}

static gboolean
on_rect_button_press (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	gfloat x = 0;
	gfloat y = 0;
	clutter_event_get_coords (event, &x, &y);

	g_print ("Clicked rectangle at (%f, %f)\n", x, y);

	return FALSE;
}

static gboolean
on_rect_button_release (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	gfloat x = 0;
	gfloat y = 0;
	clutter_event_get_coords (event, &x, &y);

	g_print ("Click-release on rectangle at (%f, %f)\n", x, y);

	return TRUE;
}

static gboolean
on_rect_enter (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	g_print ("Entered rectangle.\n");

	return TRUE;
}

static gboolean
on_rect_leave (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	g_print ("Leaved rectangle .\n");

	return TRUE;
}

static gboolean
on_rect_motion (ClutterRectangle *rect, ClutterEvent *event, gpointer data)
{
	g_print ("Motion in the rectangle .\n");

	return TRUE;
}



int main (int argc, char *argv[])
{
	ClutterColor stage_color = {0x00, 0x00, 0x00, 0xff };
	ClutterColor label_color = {0xff, 0xff, 0xff, 0x99 };

	clutter_init (&argc, &argv);

	ClutterActor *stage = clutter_stage_get_default ();
	clutter_actor_set_size (stage, 200, 200);
	clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);

	g_signal_connect (stage, "button-press-event", G_CALLBACK (on_stage_button_press), NULL);

	ClutterActor *rect = clutter_rectangle_new_with_color (&label_color);
	clutter_actor_set_size (rect, 100, 100);
	clutter_actor_set_position (rect, 50, 50);
	clutter_actor_show (rect);
	clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect);

	clutter_actor_set_reactive (rect, TRUE);

	g_signal_connect (rect, "button-press-event", G_CALLBACK (on_rect_button_press), NULL);
	g_signal_connect (rect, "button-release-event", G_CALLBACK (on_rect_button_release), NULL);
	g_signal_connect (rect, "motion-event", G_CALLBACK (on_rect_motion), NULL);
	g_signal_connect (rect, "enter-event", G_CALLBACK (on_rect_enter), NULL);
	g_signal_connect (rect, "leave-event", G_CALLBACK (on_rect_leave), NULL);

	clutter_actor_show (stage);

	clutter_main ();

	return EXIT_SUCCESS;
}
